<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>用户设置</title>
    <script type="text/javascript">
      var newEmployeeWin;
      var editEmployeeWin;
      var employeeId;
      var employeeDS;
      //处理员工锁定
      function lockEmployeeFn(id){
        Ext.Ajax.request({
          url : '/employee/employeeLock',
          success : function (result, request) {
            employeeDS.reload();
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET',
          params : {
            id : id
          }
        });
      }
      
      Ext.onReady(function(){
        //用户设置---------------------------------------------------------
        //生成有关用户的Grid
        employeeDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/employee/getAllEmployees'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'quit_job'
            }, {
              name: 'login_id'
            }, {
              name: 'identity_card'
            }, {
              name: 'phone'
            }, {
              name: 'email'
            }, {
              name: 'address'
            }, {
              name: 'department_name',
              mapping:'department',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'role_name',
              mapping:'role',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'description'
            },{
              name: 'is_locked'
            }, {
              name: 'updated_at'
            }])
        });
        var employeeSM = new Ext.grid.CheckboxSelectionModel();
        var employeeCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),employeeSM,{
              header: 'id',
              dataIndex: 'id',
              width: 30
            }, {
              header: '是否锁定',
              dataIndex: 'is_locked',
              renderer: renderEmployeeIsLucked,
              width: 60
            }, {
              header: '编号',
              dataIndex: 'number',
              width:45
            }, {
              header: '登录Id',
              dataIndex: 'login_id',
              width:70
            }, {
              header: '名称',
              dataIndex: 'name',
              width:60
            }, {
              header: '是否离职',
              dataIndex: 'quit_job',
              width:70
            }, {
              header: '部门',
              dataIndex: 'department_name',
              width:70
            }, {
              header: '角色',
              dataIndex: 'role_name',
              width:70
            }, {
              header: '身份证',
              dataIndex: 'identity_card'
            }, {
              header: '电话',
              dataIndex: 'phone'
            }, {
              header: '地址',
              dataIndex: 'address',
              width:230
            }, {
              header: '电子邮箱',
              dataIndex: 'email',
              width:180
            }, {
              header: '描述',
              dataIndex: 'description',
              hidden:true,
              width:230
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }]});

        //锁定用户
        function renderEmployeeIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<a style=cursor:pointer onclick=lockEmployeeFn(" + store.getAt(rowIndex).get('id')+")><font color=red>锁定</font></a>";
          }else{
            return "<a style=cursor:pointer onclick=lockEmployeeFn(" + store.getAt(rowIndex).get('id')+")><font color=green>未锁定</font></a>";
          }
        }
        
        var employeeGrid =  new Ext.grid.GridPanel({
          ds: employeeDS,
          cm: employeeCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-190,
          height: screenHeight-285,
          iconCls: 'icon-grid',
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '用户列表',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize + 15,
            store: employeeDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newEmployeeWin = new Ext.Window({
                  layout: 'fit',
                  width:617,
                  height:325,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加用户',
                  autoLoad: {
                    url: "/employee/newEmployee",
                    scripts: true
                  }
                });
                newEmployeeWin.on("destroy",function(){
                  employeeDS.reload();
                });
                newEmployeeWin.resizable = false;
                newEmployeeWin.show();
              }
            },'-',{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = employeeGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  employeeId = records[0].get('id');
                  editEmployeeWin = new Ext.Window({
                    title: '编辑用户',
                    layout:'fit',
                    width:617,
                    height:325,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/employee/editEmployee?id=" + employeeId,
                      scripts: true
                    }
                  });
                }
                editEmployeeWin.on("destroy",function(){
                  employeeDS.reload();
                });
                editEmployeeWin.resizable = false;
                editEmployeeWin.show();
              }
            }]
        });
        employeeGrid.render('employee_list');
        employeeDS.load();
      })
    </script>
  </head>
  <body>
    <div id="employee_list"></div>
  </body>
</html>
